<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/5/29
  Time: 8:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/wechat/home/sort.css">
    <jsp:include page="../public/head.jsp" flush="true" />
</head>
<body >
<div id="my_sort" style="position: absolute;width:100%">
    <div>
    <jsp:include page="../public/header.jsp" flush="true" />
    <div class="my_sort" >
        <!-- 导航 -->
        <!-- 单张海报 -->
        <section >



            <div class="pos_place"><a><img src="${productClass.detailImage}"></a></div>
            <!-- 区域列表 -->

            <div class="out">
                <div class="time"><i class="iconfont icon-me">&#xe626;</i><span class="s1">打烊时间:</span><span class="s2">${productClass.closetime}</span></div>
                <%--<div class="sold"><span class="s1">销量</span><span class="s2">${scout}</span></div>--%>
            </div>
            <!--/out-->
            <div class="out2">请选择送餐时间</div>
            <!--/out2-->
            <form id="week" action="/home/shopCar.do" method="post" class="out3" >
                <c:forEach items="${week}" var="item" varStatus="status">
                    <%--<div>${item.weekName} ${item.date}<input type="radio" name="week" ${item.state} value="${item.rdate}" ></div>--%>
                    <div class="cont">
                        <input class="myDate" type="radio" name="week" id="radio${status.index}" ${item.state} value="${item.rdate}" onclick="check('i${status.index}')"  onchange="change(this)" >
                        <!--&#xe61c;-->
                        <span class="s1">${item.weekName}</span><span class="s2">${item.date} <i class="icon ${item.state}" id="i${status.index}"></i></span>

                    </div>
                </c:forEach>
                <input type="hidden" name="order" id="order">
                <input  name="totalCount" type="hidden" id="totalCount">
                <input  name="totalPrice" type="hidden" id="totalPrice">
            </form>

            <div id="wrapper">
            <div class="out4" id=" scroller">

            </div>
            </div>
            <!--/out4-->
            <c:if test="${!empty products }">
            <div class="ps" id="pullRefresh" hidden>正在加载</div>
            <div class="buy">
                <div class="txt">
                    <div class="txt1"><i class="iconfont buyico">&#xe61f;<span class="no" id="count">0</span></i></div>
                    <div class="txt2"><span class="s1">总计:￥</span><span class="s2" id="total">0.0</span></div>
                </div>
                <a onclick="buy()"><div class="txt3">去结算</div></a>
            </div>
            </c:if>
        </section>
    </div>
    </div>
</div>
<script>

    var hotelNo = '${hotelNo}';
     if(hotelNo=="didi"){
         $(".out").css("display","none")
         $(".out2").css("display","none")
         $(".cont").css("display","none")

     }

    var canadd = true;

    $(function () {

        init();

        $("input[name='goodAccount']").val(0);


        var now = new Date();

        var time = new Date(now.getFullYear()+'/'+(now.getMonth()+1)+"/"+now.getDate()+" ${productClass.closetime}");



        if (now > time ){
            alert("商铺已打烊");
            canadd = false;
            $(".buy").hide();
        }
    });

    var isLast = false;
    var index=0;
    var size =100;


    function init() {
        $.post("/mobile/product?action=productType",{areaId:${area.id},classId:${productClass.id},begin:index,end:size},function(data){
            if (!data.success){
                tip("",data.msg,true);
            }else {
                if (data.data.length > 0) {
                    $("#pullRefresh").toggle();
                    var html = "";
                    $.each(data.data, function (i, value) {
                        html += '<form id="form' + (index + i) + '" >' +
                                '<div class="cont">' +
                                '<img src="' + value.image + '" onclick="productDetail(' + value.id + ',' + ${productClass.id}+ ')"      >' +
                                '<input type="hidden" value="' + value.price + '" name="goodPrcie" id="goodPrcie' + (index + i) + '" />' +
                                '<input type="hidden" value="' + value.id + '" name="goodId" />' +
                                '<input type="hidden" value="' + value.image + '" name="goodNormalImage" />' +
                                '<input type="hidden" value="' + value.name + '" name="goodName" />' +
                                '<input type="hidden" value="' +${productClass.id} +'" name="productClassId" />'+
                                '<input type="hidden" value="0" name="goodAccount" id="count' + (index + i) + '" />' +
                                '<span id="colBt">' +
                                '<i class="iconfont num _num" id="addSub' + (index + i) + '" style="display: none">' +
                                '<div class="mask" ></div>' +
                                '<div class="add _add">' +
                                '<div class="left" onclick="countSub(\'' + (index + i) + '\')" ></div>' +
                                '<span  class="center" id="scount' + (index + i) + '" >0</span>' +
                                '<div class="right" onclick="countAdd(\'' + (index + i) + '\')"></div>' +
                                '</div>' +
                                '</i>' +
                                '<i class="iconfont num _num" id="add' + (index + i) + '" onclick="countAdd(\'' + (index + i) + '\')" ></i>' +
                                '</span>' +
                                '</div>' +
                                '</form>';
                    });
                    $(".out4").append(html);
                }
            }
        },"json");
    }




    function productDetail(id,productClassId) {

        var tmp = $("#week").serializeObject();
        var date = tmp.week;
        window.location.replace("/home/beforelogin/productDetail.do?sellcount=${scout}&type=0&id="+id+"&rdate="+date+"&productClassId="+productClassId);
    }

    function countAdd(id) {
        if (!canadd){
            alert("商铺已打烊");
            return;
        }

        var current = $('#count'+id).val();
        var currentCount = $("#count").text();
        var currenttotal = $("#total").text();
        var price = $('#goodPrcie'+id).val();
        current++;
        currentCount++;
        currenttotal = (parseFloat(price)+parseFloat(currenttotal)).toFixed(1);
        $("#count"+id).val(current);
        $("#scount"+id).text(current) ;
        $("#count").text(currentCount);
        $("#total").text(currenttotal);

        if (current == 1){
            $("#add"+id).toggle();
            $("#addSub"+id).toggle();
        }
    }
    function countSub(id) {
        var current = $('#count'+id).val();
        var currentCount = $("#count").text();
        var currenttotal = $("#total").text();
        current--;
        currentCount--;
        currenttotal = (parseFloat(currenttotal) - parseFloat($('#goodPrcie'+id).val())).toFixed(1);
        $("#count"+id).val(current);
        $("#scount"+id).text(current) ;
        $("#count").text(currentCount);
        $("#total").text(currenttotal);

        if (current == 0){
            $("#add"+id).toggle();
            $("#addSub"+id).toggle();
        }
    }

    function buy(){
        var forms = $("form:not([id='week'])");
        var goods = [];
        var tmp = $("#week").serializeObject();
        var key = tmp.week;
        var size = forms.size();
//
//        var test = $("#form0").serializeObject();
//        console.log(JSON.stringify(test));

        for(var i = 0;i<size;i++){
            var temp = $("#"+forms[i].id).serializeObject();
            if (temp.goodAccount > 0) {
                temp.shopId = key;
                goods.push(temp);
//                console.log(JSON.stringify(temp));
            }
        }
//        console.log(JSON.stringify(goods));
        if (goods.length > 0) {
            var data = '{"' + key + '":' + JSON.stringify(goods) + '}';
            console.log(data);
            $("#order").val(data);
            $("#totalCount").val($("#count").text());
            $("#totalPrice").val($("#total").text());
            $("#week").submit();
        }
    }

    function check(id) {
        $("i").removeClass("checked");
        $("#"+id).addClass("checked");
    }

    function change(item){

        //现在的时间
        var now = new Date();
        //打烊的时间
        var time = new Date(now.getFullYear()+'/'+(now.getMonth()+1)+"/"+now.getDate()+" ${productClass.closetime}");

        //选择送达的时间
        var selectday = $(item).val();
        var selectdate = new Date(selectday);
        var select =  selectdate.getDate();
        //不允许的情况:   现在的时间与送达的时间一样且 打烊了
        //允许的情况
        var nextdate = new Date((now.valueOf()+1*24*60*60*1000));

        if (now > time &&  nextdate.getDate() == select){
            alert("商铺已打烊");
            canadd = false;
            $(".buy").hide();
        }else{
            $(".buy").show();
            canadd = true;
        }

    }


    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
</script>
</body>
 <script type="text/javascript" src="../public/js/JSPtag.js"></script>
</html>
